<?php

namespace maotool\util;

use maotool\exception\RuntimeException;
use maotool\object\StrUtil;

class NodeUtil
{

    /**
     * 运行 Node 文件
     * @param $path
     * @param $options
     * @return string
     */
    public static function run($path, $options = [
        "nodePath" => null,
        "nodeOptions" => [],
        "workPath" => null,
        "args" => [],
    ])
    {
        $nodePath = isset($options["nodePath"]) ? $options["nodePath"] : EnvUtil::get("NODE_PATH_BIN", "node");
        $nodeOptions = isset($options["nodeOptions"]) ? $options["nodeOptions"] : [];
        $workPath = isset($options["workPath"]) ? $options["workPath"] : null;
        $args = isset($options["args"]) ? $options["args"] : [];
        $path = PathUtil::path($path);
        $nodePath = PathUtil::path($nodePath);
        $workPath = PathUtil::path($workPath);
        if (OsUtil::isWindows()) {
            $commands = [];
            if (StrUtil::hasNotEmpty($workPath)) {
                $commands[] = "cd";
                $commands[] = $workPath;
                $commands[] = "&&";
            }
            $commands[] = $nodePath;
            if (is_array($nodeOptions)) {
                foreach ($nodeOptions as $nodeOption) {
                    $commands[] = $nodeOption;
                }
            }
            $commands[] = $path;
            if (is_array($args)) {
                foreach ($args as $arg) {
                    $commands[] = $arg;
                }
            }
            $command = StrUtil::join($commands, " ");
            exec($command, $output);
            return StrUtil::join($output, "\n");
        } else if (OsUtil::isLinux()) {
            $commands = [];
            if (StrUtil::hasNotEmpty($workPath)) {
                $commands[] = "cd";
                $commands[] = $workPath;
                $commands[] = "&&";
            }
            $commands[] = $nodePath;
            if (is_array($nodeOptions)) {
                foreach ($nodeOptions as $nodeOption) {
                    $commands[] = $nodeOption;
                }
            }
            $commands[] = $path;
            if (is_array($args)) {
                foreach ($args as $arg) {
                    $commands[] = $arg;
                }
            }
            $command = StrUtil::join($commands, " ");
            exec($command, $output);
            return StrUtil::join($output, "\n");
        } else {
            throw new RuntimeException("未知的操作系统 无法运行 Node 脚本");
        }
    }

    /**
     * 获取当前 Node 版本号
     * @return string
     */
    public static function getVersion($options = [
        "nodePath" => null,
        "nodeOptions" => [],
    ])
    {
        $nodePath = isset($options["nodePath"]) ? $options["nodePath"] : EnvUtil::get("NODE_PATH_BIN", "node");
        $nodeOptions = isset($options["nodeOptions"]) ? $options["nodeOptions"] : [];
        $nodePath = PathUtil::path($nodePath);
        if (OsUtil::isWindows()) {
            $commands = [];
            $commands[] = $nodePath;
            if (is_array($nodeOptions)) {
                foreach ($nodeOptions as $nodeOption) {
                    $commands[] = $nodeOption;
                }
            }
            $commands[] = "--version";
            $command = StrUtil::join($commands, " ");
            exec($command, $output);
            return StrUtil::join($output, "\n");
        } else if (OsUtil::isLinux()) {
            $commands = [];
            $commands[] = $nodePath;
            if (is_array($nodeOptions)) {
                foreach ($nodeOptions as $nodeOption) {
                    $commands[] = $nodeOption;
                }
            }
            $commands[] = "--version";
            $command = StrUtil::join($commands, " ");
            exec($command, $output);
            return StrUtil::join($output, "\n");
        } else {
            throw new RuntimeException("未知的操作系统 无法运行 Node 脚本");
        }
    }

    /**
     * 获取当前 Node 解释器的可执行文件路径
     * @return string
     */
    public static function getBinaryPath($options = [
        "nodePath" => null,
        "nodeOptions" => [],
    ])
    {
        $nodePath = isset($options["nodePath"]) ? $options["nodePath"] : EnvUtil::get("NODE_PATH_BIN", "node");
        $nodeOptions = isset($options["nodeOptions"]) ? $options["nodeOptions"] : [];
        $nodePath = PathUtil::path($nodePath);
        if (OsUtil::isWindows()) {
            $commands = [];
            $commands[] = $nodePath;
            if (is_array($nodeOptions)) {
                foreach ($nodeOptions as $nodeOption) {
                    $commands[] = $nodeOption;
                }
            }
            $commands[] = "--s";
            $command = StrUtil::join($commands, " ");
            exec($command, $output);
            return StrUtil::join($output, "\n");
        } else if (OsUtil::isLinux()) {
            $commands = [];
            $commands[] = $nodePath;
            if (is_array($nodeOptions)) {
                foreach ($nodeOptions as $nodeOption) {
                    $commands[] = $nodeOption;
                }
            }
            $commands[] = "--version";
            $command = StrUtil::join($commands, " ");
            exec($command, $output);
            return StrUtil::join($output, "\n");
        } else {
            throw new RuntimeException("未知的操作系统 无法运行 Node 脚本");
        }
    }

}